﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.Text;

namespace Travel.MVC
{
    public class PagerHelper
    {
        /// <summary>
        /// get page html
        /// </summary>
        /// <param name="PageUrl">page url template</param>
        /// <param name="PageQueryName">page param name, just as  'page' </param>
        /// <param name="PageIndex">current page index , 1...</param>
        /// <param name="PageSize">page size</param>
        /// <param name="TotalItems">row count</param>
        /// <param name="MaxNextPrevButtons">max next pre button num ,just as ' 5 '</param>
        /// <param name="NextText">next button text </param>
        /// <param name="PrevText">pre button text</param>
        /// <param name="ShowLastPage">show last page ?</param>
        /// <returns> div class=\"page-mod fr\"   ....   /div </returns>
        public static string GetPagerHtml(string PageUrl, string PageQueryName, int PageIndex, int PageSize, int TotalItems, int MaxNextPrevButtons, string NextText, string PrevText, bool ShowLastPage)
        {
            if (PageSize == 0)
            {
                return string.Empty;
            }
            if (string.IsNullOrEmpty(PageQueryName))
                PageQueryName = "page";
            StringBuilder sb = new StringBuilder();
            int totalPages = Convert.ToInt32(TotalItems / PageSize);

            if (TotalItems % PageSize > 0)
                totalPages++;

            if (totalPages <= 1)
                return string.Empty;

            string[] ss = PageUrl.Split('?');
            string query = "";
            if (ss.Length > 1)
            {
                string[] pms = ss[1].Split('&');
                int i = 0;
                foreach (string s in pms)
                {
                    if (s.ToLower().StartsWith(PageQueryName + "="))
                    {
                        query += ((i == 0 ? "" : "&") + PageQueryName + "={0}");
                    }
                    else
                    {
                        query += ((i == 0 ? "" : "&") + s);
                    }
                    i++;
                }
            }
            else
            {
                query = PageQueryName + "={0}";
            }
            if (query.IndexOf(PageQueryName + "=") < 0)
                query += ("&" + PageQueryName + "={0}");
            PageUrl = ss[0] + "?" + query;

            if (PageIndex > 1)
            {
                sb.Append(string.Format(" <a href='{0}' class='page-prev'>{1}</a>",
                    string.Format(PageUrl, (PageIndex - 1) > 0 ? (PageIndex - 1) : 1),
                    PrevText));
            }

            int start = (PageIndex - MaxNextPrevButtons / 2) > 0 ? (PageIndex - MaxNextPrevButtons / 2) : 1;

            int end = (PageIndex + MaxNextPrevButtons / 2) < totalPages ? (PageIndex + MaxNextPrevButtons / 2) : totalPages;
            if (start == 1)
            {
                end = MaxNextPrevButtons > totalPages ? totalPages : MaxNextPrevButtons;
            }
            else if (end == totalPages)
            {
                start = MaxNextPrevButtons > totalPages ? 1 : totalPages - MaxNextPrevButtons;
            }
            if (start < 1)
                start = 1;

            if (start > 1)
            {
                sb.Append(string.Format(" <a href='{0}'>{1}</a>",
                    string.Format(PageUrl, 1),
                    1));
            }

            if (start > 2)
            {
                sb.Append(string.Format(" <a class='page-last'>..</a>"));
            }

            for (int i = start; i <= end; i++)
            {
                if (i != PageIndex)
                {
                    sb.Append(string.Format("<a href='{0}'>{1}</a>",
                    string.Format(PageUrl, i),
                    i));
                }
                else
                {
                    sb.AppendFormat("<strong>{0}</strong>", i.ToString());
                }
            }

            if (ShowLastPage && end <= totalPages - 2)
            {
                sb.Append(string.Format(" <a class='page-last'>..</a>"));
            }

            if (end < totalPages && ShowLastPage)
            {
                sb.Append(string.Format(" <a href='{0}'>{1}</a>",
                    string.Format(PageUrl, totalPages),
                    totalPages));
            }

            if (PageIndex < totalPages)
            {
                sb.Append(string.Format(" <a href='{0}' class='page-next'>{1}</a>",
                    string.Format(PageUrl, (PageIndex + 1) < totalPages ? (PageIndex + 1) : totalPages),
                    NextText));
            }
            return "<div class=\"pager\">共 " + TotalItems + " 条记录，每页 " + PageSize + " 条，" + sb.ToString() + "</div>";
        }


    }
}

